#ifndef TABSETTINGS_H
#define TABSETTINGS_H

#include <QtCore>
#include <QtGui>

namespace CodeEdit {

/**
 * Settings that describes how the code editor behaves regarding indentation,
 * with some convenience functions for formatting.
 */
struct TabSettings
{
    //! Tab key performs auto-indent:
    enum TabKeyBehavior {
        TabNeverIndents = 0,  // Never
        TabAlwaysIndents = 1, // Always
        TabLeadingWhitespaceIndents = 2 // In leading white space
    };

    TabSettings();

    void toSettings(QSettings*) const;
    void fromSettings(QSettings*);

    bool equals(const TabSettings&) const;

    bool spacesForTabs;     // Insert spaces instead of tabs?
    bool autoSpacesForTabs; // Automatically converts tabs to spaces?
    bool autoIndent;        // Enable automatic indentation?
    bool smartBackspace;    // Backspace follows indentation?
    int tabSize;            // Tab size?
    int indentSize;         // Indent size?
    TabKeyBehavior tabKeyBehavior; // How would Tab key performs auto-indent?

    // convenience functions for formatting
    int lineIndentPosition(const QString&) const;
    int firstNonSpace(const QString&) const;
    int columnAt(const QString& text, int position) const;
    int spacesLeftFromPosition(const QString& text, int position) const;
    int indentedColumn(int column, bool doIndent = true) const;
    QString indentationString(int startColumn, int targetColumn, const QTextBlock& currentBlock = QTextBlock()) const;
    QString indentationString(const QString& text) const;
    int indentationColumn(const QString& text) const;

    bool cursorIsAtBeginingOfLine(const QTextCursor& cursor) const;

    void indentLine(QTextBlock block, int newIndent) const;
    void reindentLine(QTextBlock block, int delta) const;

    int trailingWhitespaces(const QString& text) const;
    bool isIndentationClean(const QTextBlock& block) const;
    bool tabShouldIndent(const QTextDocument* document, QTextCursor cursor, int* suggestedPosition = 0) const;
    bool guessSpacesForTabs(const QTextBlock& block) const;
};

inline bool operator==(const TabSettings& t1, const TabSettings& t2) { return t1.equals(t2); }
inline bool operator!=(const TabSettings& t1, const TabSettings& t2) { return !t1.equals(t2); }

} // namespace CodeEdit

#endif // TABSETTINGS_H
